package utils

import (
	kmodel "longblog/work/model"
)

type DelMenuId struct {
	ID int64 `json:"id"`
}

type PermissionMenu struct {
	ID      int64  `json:"id"`
	Name    string `json:"name"`
	Pid     int64  `json:"pid"`
	Level   int64  `json:"level"`
	Open    bool   `json:"open"`
	Checked bool   `json:"checked"`
}

type Menu struct {
	ID        int64  `json:"id"`
	Name      string `json:"name"`
	Pid       int64  `json:"pid"`
	Level     int64  `json:"level"`
	Path      string `json:"path"`
	IsShow    int64  `json:"is_show"`
	CreatedAt string `json:"created_at"`
}

type TreeList struct {
	ID        int64       `json:"id"`
	Name      string      `json:"name"`
	Pid       int64       `json:"pid"`
	Level     int64       `json:"level"`
	Path      string      `json:"path"`
	IsShow    int64       `json:"is_show"`
	CreatedAt string      `json:"created_at"`
	Child     []*TreeList `json:"child"`
}

func (m *Menu) MenuList(objs []kmodel.CmsAdminPermissions) []*TreeList {
	return m.getMenu(objs, 0, 0)
}

func (m *Menu) MenuMerge(objs []kmodel.CmsAdminPermissions) []*Menu {
	tmp := make([]*Menu, 0)
	list := m.getMenu(objs, 0, 0)
	for _, v := range list {
		if v.Pid == 0 {
			tmp = append(tmp, &Menu{
				ID:        v.ID,
				Name:      v.Name,
				Pid:       v.Pid,
				Level:     v.Level,
				Path:      v.Path,
				IsShow:    v.IsShow,
				CreatedAt: v.CreatedAt,
			})
			if len(v.Child) != 0 {
				tmp = append(tmp, m.concat(v.ID, v.Child)...)
			}
		}
	}
	return tmp
}

func (m *Menu) concat(id int64, list []*TreeList) []*Menu {
	tmp := make([]*Menu, 0)
	for _, v := range list {
		if id == v.Pid {
			tmp = append(tmp, &Menu{
				ID:        v.ID,
				Name:      v.Name,
				Pid:       v.Pid,
				Level:     v.Level,
				Path:      v.Path,
				IsShow:    v.IsShow,
				CreatedAt: v.CreatedAt,
			})
			if len(v.Child) != 0 {
				tmp = append(tmp, m.concat(v.ID, v.Child)...)
			}
		}
	}
	return tmp
}

func (m *Menu) DelMergeId(id int64, objs []kmodel.CmsAdminPermissions) []*DelMenuId {
	tmp := make([]*DelMenuId, 0)
	list := m.getMenu(objs, id, 0)
	for _, v := range list {
		if v.Pid == id {
			tmp = append(tmp, &DelMenuId{
				ID: v.ID,
			})
			if len(v.Child) != 0 {
				tmp = append(tmp, m.concatId(v.ID, v.Child)...)
			}
		}
	}
	return tmp
}

func (m *Menu) concatId(id int64, list []*TreeList) []*DelMenuId {
	tmp := make([]*DelMenuId, 0)
	for _, v := range list {
		if id == v.Pid {
			tmp = append(tmp, &DelMenuId{
				ID: v.ID,
			})
			if len(v.Child) != 0 {
				tmp = append(tmp, m.concatId(v.ID, v.Child)...)
			}
		}
	}
	return tmp
}

func (m *Menu) getMenu(objs []kmodel.CmsAdminPermissions, pid int64, level int64) []*TreeList {
	var menu []Menu
	var treeList []*TreeList
	count := len(objs)
	for i := 0; i < count; i++ {
		if objs[i].Pid == pid {
			temp := Menu{
				ID:        objs[i].ID,
				Name:      objs[i].Name,
				Pid:       objs[i].Pid,
				Level:     level,
				Path:      objs[i].Path,
				IsShow:    objs[i].IsShow,
				CreatedAt: FormatTime(objs[i].CreatedAt),
			}
			menu = append(menu, temp)
		}
	}
	for _, v := range menu {
		level = v.Level + 1
		child := v.getMenu(objs, v.ID, level)
		node := &TreeList{
			ID:        v.ID,
			Name:      v.Name,
			Pid:       v.Pid,
			Level:     v.Level,
			Path:      v.Path,
			IsShow:    v.IsShow,
			CreatedAt: v.CreatedAt,
		}
		node.Child = child
		treeList = append(treeList, node)
	}
	return treeList
}
